function BindTable(id, url, params, callback, openpage) {
    var pager = true;
    if (typeof openpage == "undefined") {
        pager = true;
    } else {
        pager = openpage;
    }
    var postparams = {};
    if (params) {
        postparams = params;
    }
    layui.use('table', function() {
        var table = layui.table;
        table.reload(id, {
            method: 'post',
            contentType: "application/json;charset=uft-8",
            url: GetRootPath() + url,
            page: pager?{curr: 1}:false,
            where: postparams,
            limits: [5,10,15,20],
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            height: 'full',
            parseData: function(res) { //res 即为原始返回的数据
                if (pager) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.rows, //解析数据列表
                        "reponse":res
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.obj.length, //解析数据长度
                        "data": res.data.obj, //解析数据列表
                        "reponse":res
                    };
                }

            },
            done: function(res, curr, count) {
                BindTableTool(id);
                if (callback) {
                    callback(res, curr, count);
                }
            }

        });
    });
}

function BindTableV2(id, cols, url, params, callback, openpage) {
    var pager = true;
    if (typeof openpage == "undefined") {
        pager = true;
    } else {
        pager = openpage;
    }
    var postparams = {};
    if (params) {
        postparams = params;
    }
    layui.use('table', function() {
        var table = layui.table;
        table.reload(id, {
            cols: cols,
            method: 'post',
            url: GetRootPath() + url,
            contentType: "application/json;charset=uft-8",
            page: pager?{curr: 1}:false,
            where: postparams,
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function(res) { //res 即为原始返回的数据
                if (pager) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.rows //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.obj.length, //解析数据长度
                        "data": res.data.obj //解析数据列表
                    };
                }
            },
            done: function(res, curr, count) {
                BindTableTool(id);
                if (callback) {
                    callback(res, curr, count);
                }
            }

        });
    });
}

function BindStaticTable(id, data, callback) {
    layui.use('table', function() {
        var table = layui.table;
        table.reload(id, {
            page: false,
            data:data,
            done: function(res, curr, count) {
                BindTableTool(id);
                if (callback) {
                    callback(res, curr, count);
                }
            }
        });
    });
}

function RefreshTable(id, TableData) {
    layui.table.init(id, {
        data: TableData
    });
}

function BindTableCellEdit(id,callback){
    layui.table.on('edit('+id+')', function (obj) {
        var field = obj.field //得到字段
        var value = obj.value //得到修改后的值
        var data = obj.data; //得到所在行所有键值
        var update = {};
        update[field] = value;
        obj.update(update);
        if(callback){
            callback(layui.table.getData(id))
        }
    });
}

function BindTableTool(id) {
    layui.use('table', function() {
        var table = layui.table;
        table.on('tool(' + id + ')', function(obj) {
            var data = obj.data
            var layEvent = obj.event;
            eval(layEvent)(data)
        });

    });
}
